package jaux.tank.admin.modular.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import jaux.tank.admin.core.base.controller.BaseController;
import jaux.tank.admin.core.base.tips.ErrorTip;
import jaux.tank.bean.annotion.core.Permission;
import jaux.tank.bean.constant.Const;
import jaux.tank.bean.dto.CfgDto;
import jaux.tank.bean.entity.system.Cfg;
import jaux.tank.bean.enumeration.BizExceptionEnum;
import jaux.tank.bean.exception.TankException;
import jaux.tank.bean.exception.TankExceptionEnum;
import jaux.tank.bean.vo.node.ZTreeNode;
import jaux.tank.dao.system.CfgRepository;
import jaux.tank.service.system.CfgService;
import jaux.tank.utils.StringUtils;
import jaux.tank.utils.TreeUtils;

/**
 * CfgController
 *
 * @version 2018/8/9 0009
 */

@Controller
@RequestMapping("/cfg")
public class CfgController extends BaseController {
    @Autowired
    private CfgRepository cfgRepository;
    @Autowired
    private CfgService cfgService;
    private static String PREFIX = "/system/cfg/";
    /**
     * 跳转到参数首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "cfg.html";
    }

    /**
     * 跳转到添加参数
     */
    @RequestMapping("/cfg_add")
    public String orgAdd() {
        return PREFIX + "cfg_add.html";
    }

    /**
     * 跳转到修改参数
     */
    @RequestMapping("/cfg_update/{cfgId}")
    public String orgUpdate(@PathVariable String cfgId, Model model) {
        Cfg cfg = cfgRepository.findById(cfgId).orElse(null);
        model.addAttribute("item",cfg);
        return PREFIX + "cfg_edit.html";
    }

    /**
     * 获取参数列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list() {
        return cfgRepository.findAll();
    }
    /**
     * 获取参数列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public  List<ZTreeNode> tree() {
    	 return TreeUtils.generateFlatTree(cfgRepository.findAll(), 
    			 null, 
    			 Cfg::getId, 
    			 Cfg::getCfgName, 
    			 c->c.getParentCfg()==null?null:(c.getParentCfg().getId()+""), 
    			 c->1).stream().map(node->{
    				 ZTreeNode ztn = new ZTreeNode();
    				 ztn.setId(node.getId());
    				 ztn.setName(node.getName());
    				 ztn.setpId(node.getPid()==null?null:node.getPid());
    				 return ztn;
    			 }).collect(Collectors.toList());
    }
    /**
     * 新增参数
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(CfgDto cfg) {
        cfgService.save(cfg.toEntity());
        return SUCCESS_TIP;
    }

    /**
     * 删除参数
     */
    @RequestMapping(value = "/delete")
    @Permission
    @ResponseBody
    public Object delete(@RequestParam String cfgId) {
    	int childrenCount = cfgRepository.countByParentCfgId(cfgId);
    	if(childrenCount>0) {
    		TankException gex = new TankException(BizExceptionEnum.SERVER_ERROR);
    		gex.setMessage("该参数包含子参数，无法删除");
    		throw gex;
    	}
    	cfgService.delete(cfgId);
        return SUCCESS_TIP;
    }

    /**
     * 修改参数
     */
    @PostMapping("/update")
    @ResponseBody
    public Object update(CfgDto cfg) {
    	if(StringUtils.isNullOrEmpty(cfg.getId())) {
    		return new ErrorTip(TankExceptionEnum.REQUEST_NULL.getCode(), TankExceptionEnum.REQUEST_NULL.getMessage());
    	}
    	cfgService.save(cfg.toEntity());
    	return SUCCESS_TIP;
    }

    /**
     * 参数详情
     */
    @RequestMapping(value = "/detail/{cfgId}")
    @ResponseBody
    public Object detail(@PathVariable("cfgId") String cfgId) {
        return cfgRepository.findById(cfgId).orElse(null);
    }

}